class Solution {
public:
    string countAndSay(int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n < 1) {
            return string("");
        }
        string s = "1";
        for (int i = 2; i <= n; ++i) {
            string t;
            for (int j = 0; j < s.length(); ) {
                int k = j + 1;
                while (k < s.length() && s[j] == s[k]) {
                    ++k;
                }
                t += static_cast<char>(k - j + '0');
                t += s[j];
                j = k;
            }
            s = t;
        }
        return s;
    }
};
